<template>
	<view v-if="Object.keys(goods).length !== 0">
		<!-- 轮播图 -->
		<swiper circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="item,index in goods.pics" :key="index">
				<view class="swiper-item">
					<image :src="item.pics_big" @click="preview(index)"></image>
				</view>
			</swiper-item>
		</swiper>
		<!-- 文字信息 -->
		<view class="info">
			<!-- 价格 -->
			<view class="price">
				￥{{goods.goods_price}}
			</view>
			<!-- 名称、收藏 -->
			<view class="mid">
				<view class="title">
					{{goods.goods_name}}
				</view>
				<view class="favi">
					<uni-icons color="gray" type="star" size="20"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yf">
				快递:免运费
			</view>
		</view>
		<!-- 商品详情 -->
		<rich-text :nodes="goods.goods_introduce"></rich-text>
		<!-- 商品导航 -->
		<!-- 商品导航组件 -->
		<view class="goods_nav">
			<!-- fill 控制右侧按钮的样式 -->
			<!-- options 左侧按钮的配置项 -->
			<!-- buttonGroup 右侧按钮的配置项 -->
			<!-- click 左侧按钮的点击事件处理函数 -->
			<!-- buttonClick 右侧按钮的点击事件处理函数 -->
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				goods: {},
				options: [{
					icon: 'shop',
					text: '店铺'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				// 右侧按钮组的配置对象
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		computed: {
			...mapGetters('m_cart', ['allGoodsCount'])
		},
		onLoad(options) {
			this.getGoods(options.goods_id)
		},
		methods: {
			...mapMutations('m_cart', ['updateAddCart']),
			//左侧图片
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			//右侧按钮
			buttonClick(e) {
				//点击加入购物车之后
				if (e.content.text === '加入购物车') {
					//准备精简版的商品数据
					const goods = {
						goods_id: this.goods.goods_id, // 商品的Id
						goods_name: this.goods.goods_name, // 商品的名称
						goods_price: this.goods.goods_price, // 商品的价格
						goods_count: 1, // 商品的数量
						goods_small_logo: this.goods.goods_small_logo, // 商品的图片
						goods_state: true // 商品的勾选状态
					}
					this.updateAddCart(goods)
				}
			},
			//预览轮播图
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goods.pics.map(e => {
						return e.pics_big
					})
				})
			},
			//获取商品详情
			async getGoods(goods_id) {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				//console.log(message);
				if (meta.status !== 200) return uni.$showMsg()
				message.goods_introduce = message.goods_introduce.replace(/<img/g, '<img style="display:block"')
				this.goods = message
			}
		},
		watch: {
			allGoodsCount: {
				handler(newCount) {
					this.options[1].info = newCount
				},
				immediate: true //页面加载时
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.price {
		color: #ea4350;
		font-size: 20px;
	}

	.mind {
		margin: 6px 0;
		font-size: 12px;
		display: flex;

		.favi {
			border-left: 1px solid #eee;
			width: 120px;
			display: flex;
			flex-direction: column;
			justify-content: center;
			color: gray;
		}
	}

	.yf {
		font-size: 10px;
		color: gray;
	}

	page {
		padding-bottom: 50px;
	}

	.goods_nav {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
	}
</style>
